package com.mew.chess.core.game.dyj;

import com.mew.chess.core.common.handler.ChessHandlerMapCmdType;
import com.mew.chess.core.common.handler.ChessPlayAnnotation;
import com.mew.chess.core.common.handler.ChessPlayHandler;
import com.mew.chess.core.common.handler.ChessPushMessageUtil;
import com.mew.chess.core.common.message.request.BetRequestMessage;
import com.mew.chess.core.game.dyj.game.DyjGameManager;
import com.mew.chess.core.game.dyj.game.DyjRoomManager;
import com.mew.chess.core.game.dyj.game.common.DyjConstantValue;
import com.mew.chess.core.game.dyj.game.common.DyjLinkType;
import com.mew.chess.core.game.dyj.game.common.DyjPushCMDMapType;
import com.mew.chess.core.game.dyj.game.message.BetRecordMessage;
import com.mew.chess.core.game.dyj.game.message.BetResponseMessage;
import com.mew.chess.core.game.dyj.game.message.PushBetMessage;
import com.mew.chess.core.game.dyj.game.message.UserMessage;
import com.mew.common.dc.redis.AucRedisService;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.bo.GameSession;
import com.zebra.remoting.protocol.ZebraCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

@ChessPlayAnnotation(ChessHandlerMapCmdType.DyjBet)
@Service
@Slf4j
public class DyjBetHandler implements ChessPlayHandler<BetRequestMessage> {

    @Autowired
    private AucRedisService aucRedisService;

    @Override
    public void handle(ZebraCommand<BetRequestMessage> command, GameSession session) {
        try {

            BetRequestMessage message = command.getRequest();
            if (message.getBetType() < 0 || message.getBetNumber() < 1) {
                command.sendFailureCode(ResponseCode.ParamInvalid.code());
                return;
            }

            long userId = session.getUserId();
            int roomId = session.getRoomId();
            DyjRoomManager roomManager = DyjGameManager.getInstance().getController(roomId);
            UserMessage userMessage = roomManager.getPlayer(userId);
            if (null == roomManager || null == userMessage) {
                log.error("DyjRoomManager is null userId:{} roomId: {}", userId, roomId);
                command.sendFailureCode(ResponseCode.LoginUn.code());
                return;
            }

            if (roomManager.getLinkType() != DyjLinkType.StartBet.code()) {
                log.error("DyjRoomManager stop bet userId:{} roomId: {}", userId, roomId);
                command.sendFailureCode(ResponseCode.StopBet.code());
                return;
            }
            if (userMessage.getGoldNumber() < message.getBetNumber()) {
                log.error("DyjRoomManager bet exceed the limit userId:{} roomId: {}", userId, roomId);
                command.sendFailureCode(ResponseCode.GoldNo.code());
                return;
            }
            if(userMessage.getAllBet()+message.getBetNumber()>roomManager.getMaxBetLimit()){
                log.error("DyjRoomManager bet exceed the limit userId:{} roomId: {}", userId, roomId);
                command.sendFailureCode(ResponseCode.BetMax.code());
                return;
            }

            userMessage.userBet(message.getBetType(), message.getBetNumber());
            roomManager.bet(message.getBetType(), message.getBetNumber());
            PushBetMessage res = new PushBetMessage();
            res.setChipBets(new BetRecordMessage[]{
                    BetRecordMessage.builder()
                            .uid(roomManager.isBigPlayer(userId)? userMessage.getUserId():-1)
                            .betType(message.getBetType())
                            .betNumber(message.getBetNumber())
                    .build()
            });
            res.setTotalNumber(roomManager.getTotalNumber().longValue());
            res.setTypeNumber(roomManager.getTypeNumber());
            List<GameSession> sessionList = roomManager.getOnlineUser().values().stream().filter(u -> u.getUserId() != userId).collect(Collectors.toList());
            ChessPushMessageUtil.broadcastMessage(sessionList, DyjPushCMDMapType.BetInfoMessage.code(),res);
            //参与
            aucRedisService.chessBetLock(userId, DyjConstantValue.OVER_TIME); //参与
            command.sendMessage(BetResponseMessage.builder()
                    .betType(message.getBetType())
                    .betNumber(message.getBetNumber())
                    .build());

        }catch (Exception e){
            e.printStackTrace();
            command.sendFailureCode(ResponseCode.SysException.code());
        }
    }

}
